package com.fencing_squad.homo.list;

import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.Data;
import de.hdodenhof.circleimageview.CircleImageView;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class OfferProvider extends BaseItemProvider {
    private List<Offer> list;
    private Context context;

    public OfferProvider(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public class Holder {
        Text nickname;
        Text content;
        Text status;
        Text rewardId;
        Text button;

        public Holder(Component component) {
            nickname = (Text) component.findComponentById(ResourceTable.Id_nickname);
            content = (Text) component.findComponentById(ResourceTable.Id_content);
            status = (Text) component.findComponentById(ResourceTable.Id_status);
            rewardId = (Text) component.findComponentById(ResourceTable.Id_reward);
            button = (Text) component.findComponentById(ResourceTable.Id_button);
        }
    }

    public void append(Offer offer) {
        if(list != null) {
            list.add(offer);
            notifyDataChanged();
        }
    }

    public void append(List<Offer> offers) {
        if(list != null) {
            list.addAll(offers);
            notifyDataChanged();
        }
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if(list != null && i >= 0 && i<list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        Offer item = list.get(i);
        Holder holder;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_user_offer, null, false);
            holder = new Holder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = convertComponent;
            holder = (Holder)cpt.getTag();
        }
        holder.content.setText(item.getContent());
        holder.nickname.setText(item.getNickname());
        holder.rewardId.setText(item.getRewardId());
        holder.status.setText(item.getStatus());
        holder.button.setText(item.getGetId().equals(Data.Account.uid) ? "我揭榜的" : "我悬赏的");
        return cpt;
    }
}
